#include <stdio.h>
#include <linux/sem.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#define RESOURCE 4
int main(void)
{
key_t key;
int semid;
struct sembuf sbuf={0,-1,IPC_NOWAIT};
union semun arg;
if((key=ftok("/home/abby",'c'))==-1) /*convert a pathname and a project identifier to a System V IPC key */
{
perror("ftok failed!\n");
exit(1);
}
if((semid=semget(key,1,IPC_CREAT|0660))==-1)
{
perror("semget failed!\n");
exit(1);
}
arg.val=RESOURCE;
printf("there are %d avliable resource !\n",arg.val);
if(semctl(semid,0,SETVAL,arg)==-1)
{
perror("semtcl failed !\n");
exit(1);
}
while(1)
{
if(semop(semid,&sbuf,1)==-1)
{
perror("semop failed!\n");
exit(1);
}
sleep(3);
}
semctl(semid,0,IPC_RMID,0);
exit(0);
}
